using UnityEngine;

public class Singleton<T> where T : new()
{
    private static T instance;
    public static T Instance
    {
        get { return Equals(instance, default(T)) ? (instance = new T()) : instance; }
    }

}


public static class SingleHelper
{
    public static T2 CreateBehaviourSingleton<T2>(ref T2 t) where T2 : MonoBehaviour
    {
        if (t == null || t.gameObject == null)
        {
            t = (T2)GameObject.FindObjectOfType(typeof(T2));

            if (GameObject.FindObjectsOfType(typeof(T2)).Length > 1)
            {
                GameObject.DontDestroyOnLoad(t.gameObject);
                return t;
            }

            if (t == null)
            {
                GameObject singleton = new GameObject();
                t = singleton.AddComponent<T2>();
                singleton.name = "(singleton) " + typeof(T2).ToString();

                GameObject.DontDestroyOnLoad(t.gameObject);
            }
            else
            {
                GameObject.DontDestroyOnLoad(t.gameObject);
            }
        }
        return t;
    }
}
/// <summary>
/// Be aware this will not prevent a non singleton constructor
/// such as `T myT = new T();`
/// To prevent that, add `protected T () {}` to your singleton class.
/// 
/// As a note, this is made as MonoBehaviour because we need Coroutines.
/// </summary>
public class BehaviourSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T _instance;

    private static object _lock = new object();



    public static T Instance
    {
        get
        {
            if (applicationIsQuitting)
            {
                return null;
            }

            lock (_lock)
            {
                SingleHelper.CreateBehaviourSingleton<T>(ref _instance);
                // if (_instance == null || _instance.gameObject == null)
                // {
                // _instance = (T)FindObjectOfType(typeof(T));
                //
                // if (FindObjectsOfType(typeof(T)).Length > 1)
                // {
                // if(EvonyDebug.enableLog)EvonyDebug.LogError("[Singleton] Something went really wrong " +
                // " - there should never be more than 1 singleton!" +
                // " Reopenning the scene might fix it." + "\n");
                // DontDestroyOnLoad(_instance.gameObject);
                // return _instance;
                // }
                //
                // if (_instance == null)
                // {
                // GameObject singleton = new GameObject();
                // _instance = singleton.AddComponent<T>();
                // singleton.name = "(singleton) " + typeof(T).ToString();
                //
                // DontDestroyOnLoad(singleton);
                //
                // if(EvonyDebug.enableLog)EvonyDebug.Log("[Singleton] An instance of " + typeof(T) +
                // " is needed in the scene, so '" + singleton +
                // "' was created with DontDestroyOnLoad." + "\n");
                // }
                // else
                // {
                // if(EvonyDebug.enableLog)EvonyDebug.Log("[Singleton] Using instance already created: " +
                // _instance.gameObject.name + "\n");
                // DontDestroyOnLoad(_instance.gameObject);
                // }
                // }

                return _instance;
            }
        }
    }


    private static bool applicationIsQuitting = false;
    /// <summary>
    /// When Unity quits, it destroys objects in a random order.
    /// In principle, a Singleton is only destroyed when application quits.
    /// If any script calls Instance after it have been destroyed, 
    /// it will create a buggy ghost object that will stay on the Editor scene
    /// even after stopping playing the Application. Really bad!
    /// So, this was made to be sure we're not creating that buggy ghost object.
    /// </summary>
    public void OnDestroy()
    {
        applicationIsQuitting = true;
    }
}

public class RefSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T _instance;

    private static object _lock = new object();

    public static T Instance
    {
        get
        {
            lock (_lock)
            {
                if (_instance == null || _instance.gameObject == null)
                {
                    _instance = (T)FindObjectOfType(typeof(T));

                    if (FindObjectsOfType(typeof(T)).Length > 1)
                    {
                        return _instance;
                    }

                    if (_instance != null)
                    {
                    }
                }

                return _instance;
            }
        }
    }

    private static bool applicationIsQuitting = false;
    /// <summary>
    /// When Unity quits, it destroys objects in a random order.
    /// In principle, a Singleton is only destroyed when application quits.
    /// If any script calls Instance after it have been destroyed, 
    /// it will create a buggy ghost object that will stay on the Editor scene
    /// even after stopping playing the Application. Really bad!
    /// So, this was made to be sure we're not creating that buggy ghost object.
    /// </summary>
    public void OnDestroy()
    {
        applicationIsQuitting = true;
    }
}
